#include "AudioManager.h"

using namespace Game;

AudioManager* AudioManager::mInstance;

AudioManager::AudioManager()
{
	Mix_Volume(-1,20);
	OnLoad(Game::Audio::SOUND_CLICK,"resources/sounds/click.wav");
	OnLoad(Game::Audio::SOUND_INTRO,"resources/sounds/Intro.ogg");
	OnLoad(Game::Audio::SOUND_MENU,"resources/sounds/Menu2.wav");
	OnLoad(Game::Audio::SOUND_MOUSE_OVER,"resources/sounds/MouseHover.ogg");
	OnLoad(Game::Audio::SOUND_MATCH,"resources/sounds/match.ogg");
	OnLoad(Game::Audio::SOUND_WRONG,"resources/sounds/wrong.wav");
	OnLoad(Game::Audio::SOUND_PLAY,"resources/sounds/play.wav");
	OnLoad(Game::Audio::SOUND_CLINK,"resources/sounds/clink.ogg");
}

bool AudioManager::OnLoad(unsigned int index,char *p_file)
{
	Mix_Chunk* TempSound = NULL;
    if ((TempSound = Mix_LoadWAV_RW(SDL_RWFromFile( p_file, "r" ), 1 )) == NULL)
    {
        return 0;
    }
    SoundList.push_back(TempSound);	
	return 1;
}

void AudioManager::PlaySound(unsigned int iD)
{
    if (iD < 0 || iD >= SoundList.size()) 
		return;
    if (SoundList[iD] == NULL) 
		return;
    Mix_PlayChannel(iD, SoundList[iD], 0);
}


void AudioManager::PlaySound(unsigned int iD,unsigned int dur)
{
    if (iD < 0 || iD >= SoundList.size()) return;
    if (SoundList[iD] == NULL) return;
	Mix_PlayChannelTimed(iD, SoundList[iD], 0,dur);
}

void AudioManager::PlaySoundLoops(unsigned int iD,unsigned int loop)
{
    if (iD < 0 || iD >= SoundList.size()) return;
    if (SoundList[iD] == NULL) return;
	Mix_PlayChannelTimed(iD, SoundList[iD], loop,-1);
}


void AudioManager::OnCleanup()
{
	for (unsigned int i = 0; i < SoundList.size(); i++)
    {
        Mix_FreeChunk(SoundList[i]);
    }
    SoundList.clear();
}

AudioManager& AudioManager::GetInstance()
{
	if(mInstance==0)
	{
		mInstance=new AudioManager();

	}
	return *mInstance;
}

void AudioManager::PauseSound(unsigned int iD)
{
	Mix_Pause(iD);
}